/*------------------------------------------------------
                hou_elearningGEN beta v1.0
Author: xtrung.net@gmail.com
Write On: 04/27/2008
Create On:4/13/2012 11:22:09 AM
------------------------------------------------------*/
using hou_elearning.Entity;
using hou_elearning.DataAccess;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Text;
namespace hou_elearning.Business
{
    public class NhatkyhocBRL
    {
        #region Init
        private static string EX_NOT_EXIST="Không Tồn Tại Nhatkyhoc Này";
		private static string EX_FK_IKHOAHOCID_INVALID="FK_iKhoahocID không hợp lệ";
		private static string EX_FK_IBAIHOCID_INVALID="FK_iBaihocID không hợp lệ";
		private static string EX_DNGAYHOC_INVALID="dNgayhoc không hợp lệ";
		private static string EX_ID_INVALID="PK_iNhatkyhocID không hợp lệ";
        #endregion
        #region Public Methods
        /// <summary>
        /// Get Nhatkyhoc Theo ID
        /// </summary>
        /// <param name="PK_iNhatkyhocID">Int64:Nhatkyhoc ID</param>
        /// <returns>NhatkyhocEntity</returns>        
        public static NhatkyhocEntity GetOne(Int64 PK_iNhatkyhocID)
        {
            
			if(PK_iNhatkyhocID<=0)
				throw new Exception(EX_ID_INVALID);
            return NhatkyhocDAL.GetOne(PK_iNhatkyhocID);
        }
        /// <summary>
        /// Lấy về List các Nhatkyhoc
        /// </summary>
        /// <returns>List NhatkyhocEntity:List Nhatkyhoc Cần lấy</returns>
        public static List<NhatkyhocEntity> GetAll()
        {
            return NhatkyhocDAL.GetAll();
        }
        public static List<NhatkyhocEntity> GetByFK_iKhoahocID(Int64 FK_iKhoahocID)
		{
			if(FK_iKhoahocID<=0)
				throw new Exception(EX_FK_IKHOAHOCID_INVALID);
			return NhatkyhocDAL.GetByFK_iKhoahocID(FK_iKhoahocID);
		}public static List<NhatkyhocEntity> GetByFK_iBaihocID(Int64 FK_iBaihocID)
		{
			if(FK_iBaihocID<=0)
				throw new Exception(EX_FK_IBAIHOCID_INVALID);
			return NhatkyhocDAL.GetByFK_iBaihocID(FK_iBaihocID);
		}
        /// <summary>
        /// Kiểm tra và thêm mới Nhatkyhoc
        /// </summary>
        /// <param name="entity">Entity</param>
        /// <returns>Int32: ID của Nhatkyhoc Mới Thêm Vào</returns>
        public static Int32 Add(NhatkyhocEntity entity)
        {
            checkLogic(entity);
            checkDuplicate(entity, false);
            checkFK(entity);
            return NhatkyhocDAL.Add(entity);
        }
        /// <summary>
        /// Kiểm tra và chỉnh sửa Nhatkyhoc
        /// </summary>
        /// <param name="entity">NhatkyhocEntity</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Edit(NhatkyhocEntity entity)
        {
            checkExist(entity.PK_iNhatkyhocID);
            checkLogic(entity);
            checkDuplicate(entity, true);
            checkFK(entity);
            return NhatkyhocDAL.Edit(entity);
        }
        /// <summary>
        /// Kiểm tra và xoá Nhatkyhoc
        /// </summary>
        /// <param name="PK_iNhatkyhocID">Int64 : PK_iNhatkyhocID</param>
        /// <returns>bool:kết quả thực hiện</returns>
        public static bool Remove(Int64 PK_iNhatkyhocID)
        {
            checkExist(PK_iNhatkyhocID);
            return NhatkyhocDAL.Remove(PK_iNhatkyhocID);
        }
        #endregion
        #region Private Methods
        private static void checkExist(Int64 PK_iNhatkyhocID)
        {
            NhatkyhocEntity oNhatkyhoc=NhatkyhocDAL.GetOne(PK_iNhatkyhocID);
            if(oNhatkyhoc==null)
                throw new Exception(EX_NOT_EXIST);
        }
        /// <summary>
        /// Kiểm tra logic Entity
        /// </summary>
        /// <param name="entity">NhatkyhocEntity: entity</param>
        private static void checkLogic(NhatkyhocEntity entity)
        {   
			if (entity.FK_iKhoahocID < 0)
				throw new Exception(EX_FK_IKHOAHOCID_INVALID);
			if (entity.FK_iBaihocID < 0)
				throw new Exception(EX_FK_IBAIHOCID_INVALID);
			if (DateTime.Parse("1753-01-01")>entity.dNgayhoc)
				throw new Exception(EX_DNGAYHOC_INVALID);
        }
        /// <summary>
        /// Kiểm tra trùng lặp bản ghi
        /// </summary>
        /// <param name="entity">NhatkyhocEntity: NhatkyhocEntity</param>
        private static void checkDuplicate(NhatkyhocEntity entity,bool checkPK)
        {
            /* 
            Example
            List<NhatkyhocEntity> list = NhatkyhocDAL.GetAll();
            if (list.Exists(
                delegate(NhatkyhocEntity oldEntity)
                {
                    bool result =oldEntity.FIELD.Equals(entity.FIELD, StringComparison.OrdinalIgnoreCase);
                    if(checkPK)
                        result=result && oldEntity.PK_iNhatkyhocID != entity.PK_iNhatkyhocID;
                    return result;
                }
            ))
            {
                list.Clear();
                throw new Exception(EX_FIELD_EXISTED);
            }
            */
        }
        /// <summary>
        /// Kiểm tra tồn tại khóa ngoại
        /// </summary>
        /// <param name="entity">NhatkyhocEntity:entity</param>
        private static void checkFK(NhatkyhocEntity entity)
        {            
			KhoahocEntity oKhoahoc = KhoahocDAL.GetOne(entity.FK_iKhoahocID);
			if (oKhoahoc==null)
			{
				throw new Exception("Không tìm thấy :FK_iKhoahocID");
			}
			BaihocEntity oBaihoc = BaihocDAL.GetOne(entity.FK_iBaihocID);
			if (oBaihoc==null)
			{
				throw new Exception("Không tìm thấy :FK_iBaihocID");
			}
        }
        #endregion
    }
}
